package com.yang.job.infra.utils;


import com.cronutils.model.CronType;
import com.cronutils.model.definition.CronDefinition;
import com.cronutils.model.definition.CronDefinitionBuilder;
import com.cronutils.model.time.ExecutionTime;
import com.cronutils.parser.CronParser;

import java.time.ZonedDateTime;
import java.util.Optional;

public class CronUtils {
    private static final CronDefinition CRON_DEFINITION = CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ);
    private static final CronParser CRON_PARSER = new CronParser(CRON_DEFINITION);

    public static ZonedDateTime nextExecutionTime(String cron, ZonedDateTime startTime) {
        ExecutionTime executionTime = ExecutionTime.forCron(CRON_PARSER.parse(cron));
        Optional<ZonedDateTime> zonedDateTime = executionTime.nextExecution(startTime);
        return zonedDateTime.get();
    }
}

